﻿#include "widget.h"
#include "ui_widget.h"
#include"unitrollmake.h"
#include"editroll.h"

#include"unitsearch.h"
#include<QtSql>
#include<QtGui>
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QSqlError>
#include<QByteArray>
#include <QDomDocument>
#include <QDomProcessingInstruction>
#include <QDomElement>
#include <QLineEdit>
#include <QString>
#include <QStringList>
#include <QFile>
#include <QFileDialog>
#include <QTextEdit>
#include <QTextStream>
#include <QTableWidget>
#include <QXmlStreamWriter>


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);



//      static QSqlQueryModel *model = new QSqlQueryModel(ui->tableview);
//      static QSqlQueryModel *model_2 = new QSqlQueryModel(ui->tableView_2);
//      static QSqlQueryModel *model_3 = new QSqlQueryModel(ui->RollInform);

//      model->setQuery(QString("select * from unit"));
//      model_2->setQuery(QString("select * from CadreInfo"));
//      model_3->setQuery(QString("select * from Roll"));

////          model_3->setHeaderData(0,Qt::Horizontal,tr("名册名称"));
////          model_3->setHeaderData(1,Qt::Horizontal,tr("日期"));

////      model->setHeaderData(0,Qt::Horizontal,QObject::tr("hahaha"));
////      model->setHeaderData(1,Qt::Horizontal,QObject::tr("说明"));
////      model->setHeaderData(2,Qt::Horizontal,QObject::tr("类型"));
////      model->setHeaderData(2,Qt::Horizontal,QObject::tr("编制情况"));
//      ui->tableview->setModel(model);
//      ui->tableview->setColumnWidth(0,127);
//      ui->tableview->setColumnWidth(1,270);
//      ui->tableview->setColumnWidth(2,150);
//      ui->tableview->setColumnWidth(3,200);
//      ui->tableView_2->setModel(model_2);
//      ui->RollInform->setModel(model_3);

    //单位表格式
    ui->unit->setColumnWidth(0,100);
    ui->unit->setColumnWidth(1,300);
    ui->unit->setColumnWidth(2,100);
    ui->unit->setColumnWidth(3,273);


//不可编辑+选中整行
    ui->RollInform->setSelectionBehavior ( QAbstractItemView::SelectRows);
    ui->RollInform->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->RollInform->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->unit->setSelectionBehavior ( QAbstractItemView::SelectRows);
    ui->unit->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->unit->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->cadre->setSelectionBehavior ( QAbstractItemView::SelectRows);
    ui->cadre->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->cadre->setEditTriggers(QAbstractItemView::NoEditTriggers);


    //设置可以多选
    ui->unit->setSelectionMode(QAbstractItemView::ExtendedSelection);
    ui->cadre->setSelectionMode(QAbstractItemView::ExtendedSelection);

    QSqlQuery r;
    QString roll=QString("select Rollname,RollDate,RollID from Roll where RollType='0'");
    r.exec(roll);
    while (r.next()) {
        int rowcount=ui->RollInform->rowCount();
        ui->RollInform->insertRow(rowcount);
        QString a=r.value(0).toString();
        ui->RollInform->setItem(rowcount,1,new QTableWidgetItem(a));
        QString b=r.value(1).toString();
        ui->RollInform->setItem(rowcount,0,new QTableWidgetItem(b));
        QString c=r.value(2).toString();
        ui->RollInform->setItem(rowcount,2,new QTableWidgetItem(c));


   }

    int i=ui->RollInform->rowCount();
    ui->Rollnumshow->setText(QString::number(i));

    ui->RollInform->setAutoScroll(false);
    ui->RollInform->setColumnWidth(0,100);
    ui->RollInform->setColumnWidth(1,110);



//    connect(umk,SIGNAL(ccc()),this,SLOT(showrollslot()));







}



//显示函数
void Widget::showroll()
{
     ui->RollInform->setRowCount(0);
    QSqlQuery r;
    QString roll=QString("select Rollname,RollDate,RollID from Roll where RollType='0'");
    r.exec(roll);
    while (r.next()) {
        int rowcount=ui->RollInform->rowCount();
        ui->RollInform->insertRow(rowcount);
        QString a=r.value(0).toString();
        ui->RollInform->setItem(rowcount,1,new QTableWidgetItem(a));
        QString b=r.value(1).toString();
        ui->RollInform->setItem(rowcount,0,new QTableWidgetItem(b));
        QString c=r.value(2).toString();
        ui->RollInform->setItem(rowcount,2,new QTableWidgetItem(c));
}
}
void Widget::showrollslot()
{
    showroll();
}

Widget::~Widget()
{
    delete ui;
}

//添加名册
void Widget::on_AddRoll_clicked()
{
    urm = new unitrollmake;
    connect(urm,SIGNAL(ccc()),this,SLOT(showrollslot()));
    urm->show();
}

//编辑/锁定
void Widget::on_Edit_clicked()
{
    QString name=ui->Edit->text();

    if (name=="编辑") {
        ui->AddRoll->setEnabled(true);
        ui->EditRoll->setEnabled(true);
        ui->DeleteRoll->setEnabled(true);
        ui->Edit->setText("锁定");

    }
    if (name=="锁定") {

        ui->AddRoll->setEnabled(false);
        ui->EditRoll->setEnabled(false);
        ui->DeleteRoll->setEnabled(false);
        ui->Edit->setText("编辑");

    }

}

//删除名册
void Widget::on_DeleteRoll_clicked()
{
    int index=ui->RollInform->currentRow();
    if (index==-1) {
        QMessageBox::critical(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("请选中要删除的名册!"));
    } else {

        int ok = QMessageBox::warning(this ,trUtf8("删除当前名册！"),trUtf8("确定删除？"),
                                      QMessageBox::Yes,QMessageBox::No);
        if (ok==QMessageBox::Yes) {
            QString rollid=ui->RollInform->item(index,2)->text();


            QSqlQuery q;
            QString del =QString("delete from Roll where RollID='%1'").arg(rollid);
            q.exec(del);

            ui->RollInform->removeRow(index);
        } else {

        }

                showroll();

    }



}


//点击名册列表
void Widget::on_RollInform_cellClicked(int row, int column)

{

    ui->unit->setRowCount(0);
    ui->cadre->setRowCount(0);
      int i=ui->RollInform->currentRow();
      if (i>-1) {

          QString RollID=ui->RollInform->item(i,2)->text();
          QString RollID_1=ui->RollInform->item(i,2)->text();
          QSqlQuery q;
          QSqlQuery c;
          QString unitmenu=QString ("select unitname,description,UnitOfCategoryName,CONCAT(adminestcount1,'人') from  unit,Roll,UnitRollRelation,UnitOfCategoryCode where  UnitRollRelation.unitid=unit.unitid and UnitRollRelation.RollID=Roll.Rollid and unit.unitcategory=UnitOfCategoryCode.UnitOfCategoryId and UnitRollRelation.RollID='%1'").arg(RollID.toInt());
          QString cadremenu= QString("select CadreName,CadreInfo.idnum,postname,posttime,rankname,ranktime,birth,sex,AdminAreaCode.AdminArea ,joinworktime from CadreRollRelation,Post,RankCode, CadreInfo left join AdminAreaCode on CadreInfo.NativePlace=AdminAreaCode.AdminAreaID   where CadreRollRelation.idnum=CadreInfo.idnum and CadreInfo.IDNum=Post.IDNum and CadreInfo.RankID=RankCode.RankID and  CadreRollRelation.RollID='%1'").arg(RollID_1.toInt());
          q.exec(unitmenu);
          while(q.next())
              {
                  int rowcount=ui->unit->rowCount();
                  ui->unit->insertRow(rowcount);
                  QString p=q.value(0).toString();
                  ui->unit->setItem(rowcount,0,new QTableWidgetItem(p));

                  QString o=q.value(1).toString();
                  ui->unit->setItem(rowcount,1,new QTableWidgetItem(o));

                  QString i=q.value(2).toString();
                  ui->unit->setItem(rowcount,2,new QTableWidgetItem(i));

                  QString u=q.value(3).toString();
                  ui->unit->setItem(rowcount,3,new QTableWidgetItem(u));
              }

          c.exec(cadremenu);

          while(c.next())
              {
                  int rowcount=ui->cadre->rowCount();
                  ui->cadre->insertRow(rowcount);
                  QString p=c.value(0).toString();
                  ui->cadre->setItem(rowcount,0,new QTableWidgetItem(p));

                  QString o=c.value(1).toString();
                  ui->cadre->setItem(rowcount,1,new QTableWidgetItem(o));

                  QString i=c.value(2).toString();
                  ui->cadre->setItem(rowcount,2,new QTableWidgetItem(i));

                  QString u=c.value(3).toString();
                  ui->cadre->setItem(rowcount,3,new QTableWidgetItem(u));
                  QString a=c.value(4).toString();
                  ui->cadre->setItem(rowcount,4,new QTableWidgetItem(a));

                  QString b=c.value(5).toString();
                  ui->cadre->setItem(rowcount,5,new QTableWidgetItem(b));



                  QString d=c.value(7).toString();
                  if (d.toInt()==1) {
                       ui->cadre->setItem(rowcount,7,new QTableWidgetItem("男"));
                  }
                  if (d.toInt()==0) {
                       ui->cadre->setItem(rowcount,7,new QTableWidgetItem("女"));
                  }


                  QString e=c.value(6).toString();
                  ui->cadre->setItem(rowcount,6,new QTableWidgetItem(e));

                  QString f=c.value(8).toString();
                  ui->cadre->setItem(rowcount,8,new QTableWidgetItem(f));
                  QString g=c.value(9).toString();
                  ui->cadre->setItem(rowcount,9,new QTableWidgetItem(g));
              }

//
      }

      int renshu=ui->cadre->rowCount();
      ui->renshu->setText(QString::number(renshu));

      int dws=ui->unit->rowCount();
      ui->unitnumshow->setText(QString::number(dws));

      ui->dangqian->setText("当前：");



}

//点击单位列表
void Widget::on_unit_cellClicked(int row, int column)
{

    QList<QTableWidgetSelectionRange>ranges = ui->unit->selectedRanges();
    //int rownum;


    ui->cadre->setRowCount(0);
    int count=ranges.count();

    for(int i=0;i<count;i++)

    {

        int topRow=ranges.at(i).topRow();
        int bottomRow=ranges.at(i).bottomRow();
        for(int j=topRow;j<=bottomRow;j++)
        {

            //rownum=j;
        int c=ui->RollInform->currentRow();
        QString unitname = ui->unit->item(j,0)->text();
        QString rollid=ui->RollInform->item(c,2)->text();
        QString cadreinfo=QString::fromUtf8 ("select  CadreName,CadreInfo.idnum,postname,posttime,rankname,ranktime,birth,sex,AdminAreaCode.AdminArea ,joinworktime from unit,CadreRollRelation,Post,RankCode, CadreInfo left join AdminAreaCode on CadreInfo.NativePlace=AdminAreaCode.AdminAreaID   where unit.unitid=CadreInfo.unitid and CadreInfo.IDNum=Post.IDNum and CadreInfo.RankID=RankCode.RankID and CadreRollRelation.idnum=CadreInfo.idnum and unit.unitname='%1' and CadreRollRelation.rollid='%2'").arg(unitname).arg(rollid);
        QSqlQuery ci;
        ci.exec(cadreinfo);
        while(ci.next())
        {
            int rowcount=ui->cadre->rowCount();
            ui->cadre->insertRow(rowcount);
            QString p=ci.value(0).toString();
            ui->cadre->setItem(rowcount,0,new QTableWidgetItem(p));

            QString o=ci.value(1).toString();
            ui->cadre->setItem(rowcount,1,new QTableWidgetItem(o));

            QString i=ci.value(2).toString();
            ui->cadre->setItem(rowcount,2,new QTableWidgetItem(i));

            QString u=ci.value(3).toString();
            ui->cadre->setItem(rowcount,3,new QTableWidgetItem(u));
            QString a=ci.value(4).toString();
            ui->cadre->setItem(rowcount,4,new QTableWidgetItem(a));

            QString b=ci.value(5).toString();
            ui->cadre->setItem(rowcount,5,new QTableWidgetItem(b));



            QString d=ci.value(7).toString();
            if (d.toInt()==1) {
                ui->cadre->setItem(rowcount,7,new QTableWidgetItem("男"));
            }
            if (d.toInt()==0) {
                ui->cadre->setItem(rowcount,7,new QTableWidgetItem("女"));
            }


            QString e=ci.value(6).toString();
            ui->cadre->setItem(rowcount,6,new QTableWidgetItem(e));

            QString f=ci.value(8).toString();
            ui->cadre->setItem(rowcount,8,new QTableWidgetItem(f));

            QString g=ci.value(9).toString();
            ui->cadre->setItem(rowcount,9,new QTableWidgetItem(g));
        }

        }
    }



    int renshu=ui->cadre->rowCount();
    ui->renshu->setText(QString::number(renshu));

}


void Widget::on_Search_clicked()
{
    ui->RollInform->setRowCount(0);

    QString rname=ui->RollName->text();
    QString uname=ui->UnitRoll->text();
    QString cname=ui->Name->text();
    //按名册查询


    if(!rname.isEmpty() and uname.isEmpty() and cname.isEmpty()  ){
    QSqlQuery r;
    QString roll=QString::fromUtf8("select Rollname,RollDate,RollID from Roll where RollType='0' and RollName like '%%1%'").arg(rname);
    r.exec(roll);
    while (r.next()) {
        int rowcount=ui->RollInform->rowCount();
        ui->RollInform->insertRow(rowcount);
        QString a=r.value(0).toString();
        ui->RollInform->setItem(rowcount,1,new QTableWidgetItem(a));
        QString b=r.value(1).toString();
        ui->RollInform->setItem(rowcount,0,new QTableWidgetItem(b));
        QString c=r.value(2).toString();
        ui->RollInform->setItem(rowcount,2,new QTableWidgetItem(c));
    }
    }

    //按单位查询

    if(!uname.isEmpty() and rname.isEmpty() and cname.isEmpty()){
    QSqlQuery u;
    QString unit=QString::fromUtf8("select Rollname,RollDate,Roll.RollID from Roll,unit,UnitRollRelation where RollType='0' and unit.unitid=UnitRollRelation.UnitID and UnitRollRelation.RollID=Roll.RollID and unit.unitname like '%%1%'").arg(uname);
    u.exec(unit);
    while (u.next()) {
        int rowcount=ui->RollInform->rowCount();
        ui->RollInform->insertRow(rowcount);
        QString a=u.value(0).toString();
        ui->RollInform->setItem(rowcount,1,new QTableWidgetItem(a));
        QString b=u.value(1).toString();
        ui->RollInform->setItem(rowcount,0,new QTableWidgetItem(b));
        QString c=u.value(2).toString();
        ui->RollInform->setItem(rowcount,2,new QTableWidgetItem(c));
    }
}
    //按干部查询

    if(!cname.isEmpty() and uname.isEmpty() and rname.isEmpty()){
        QSqlQuery c;
        QString cadre=QString::fromUtf8("select Rollname,RollDate,Roll.RollID from Roll,CadreRollRelation,CadreInfo where RollType='0' and CadreInfo.idnum=CadreRollRelation.idnum and CadreRollRelation.RollID=Roll.RollID and CadreInfo.CadreName='%1'").arg(cname);
        c.exec(cadre);
        while (c.next()) {
            int rowcount=ui->RollInform->rowCount();
            ui->RollInform->insertRow(rowcount);
            QString a=c.value(0).toString();
            ui->RollInform->setItem(rowcount,1,new QTableWidgetItem(a));
            QString b=c.value(1).toString();
            ui->RollInform->setItem(rowcount,0,new QTableWidgetItem(b));
            QString d=c.value(2).toString();
            ui->RollInform->setItem(rowcount,2,new QTableWidgetItem(d));
        }

    }

    //按干部单位查询

    if(!cname.isEmpty() and !uname.isEmpty() and rname.isEmpty()){
        QSqlQuery c;
        QString cadre=QString::fromUtf8("select Rollname,RollDate,Roll.RollID from Roll,CadreRollRelation,UnitRollRelation,CadreInfo,unit where RollType='0' and CadreInfo.idnum=CadreRollRelation.idnum and UnitRollRelation.UnitID=unit.unitid and CadreRollRelation.RollID=Roll.RollID and unit.unitid=UnitRollRelation.unitid and UnitRollRelation.RollID=Roll.RollID and CadreInfo.CadreName ='%1' and unit.unitname like '%%2%' ").arg(cname).arg(uname);
        c.exec(cadre);
        while (c.next()) {
            int rowcount=ui->RollInform->rowCount();
            ui->RollInform->insertRow(rowcount);
            QString a=c.value(0).toString();
            ui->RollInform->setItem(rowcount,1,new QTableWidgetItem(a));
            QString b=c.value(1).toString();
            ui->RollInform->setItem(rowcount,0,new QTableWidgetItem(b));
            QString d=c.value(2).toString();
            ui->RollInform->setItem(rowcount,2,new QTableWidgetItem(d));
        }



    }

    //按名册单位查询

    if(cname.isEmpty() and !uname.isEmpty() and !rname.isEmpty() ){
        QSqlQuery c;
        QString cadre=QString::fromUtf8("select Rollname,RollDate,Roll.RollID from Roll,unit,UnitRollRelation where RollType='0' and unit.unitid=UnitRollRelation.UnitID and UnitRollRelation.RollID=Roll.RollID and unit.unitname like '%%2%' and Roll.RollName like '%%1%'  ").arg(rname).arg(uname);
        c.exec(cadre);
        while (c.next()) {
            int rowcount=ui->RollInform->rowCount();
            ui->RollInform->insertRow(rowcount);
            QString a=c.value(0).toString();
            ui->RollInform->setItem(rowcount,1,new QTableWidgetItem(a));
            QString b=c.value(1).toString();
            ui->RollInform->setItem(rowcount,0,new QTableWidgetItem(b));
            QString d=c.value(2).toString();
            ui->RollInform->setItem(rowcount,2,new QTableWidgetItem(d));
        }

    }


   //按名册干部查询
    if(!cname.isEmpty() and uname.isEmpty() and !rname.isEmpty() ){
        QSqlQuery c;
        QString cadre=QString::fromUtf8("select Rollname,RollDate,Roll.RollID from Roll,CadreRollRelation,CadreInfo where RollType='0' and CadreInfo.idnum=CadreRollRelation.idnum and CadreRollRelation.RollID=Roll.RollID and CadreInfo.CadreName ='%2' and Roll.RollName like '%%1%'  ").arg(rname).arg(cname);
        c.exec(cadre);
        while (c.next()) {
            int rowcount=ui->RollInform->rowCount();
            ui->RollInform->insertRow(rowcount);
            QString a=c.value(0).toString();
            ui->RollInform->setItem(rowcount,1,new QTableWidgetItem(a));
            QString b=c.value(1).toString();
            ui->RollInform->setItem(rowcount,0,new QTableWidgetItem(b));
            QString d=c.value(2).toString();
            ui->RollInform->setItem(rowcount,2,new QTableWidgetItem(d));
        }

    }

//组合查询
    if(!cname.isEmpty() and !uname.isEmpty() and !rname.isEmpty() ){
        QSqlQuery c;
        QString cadre=QString::fromUtf8("select Rollname,RollDate,Roll.RollID from Roll,UnitRollRelation,CadreRollRelation,CadreInfo,unit where RollType='0' and  unit.unitid=UnitRollRelation.unitid and UnitRollRelation.RollID=Roll.RollID and CadreRollRelation.idnum=CadreInfo.idnum and CadreRollRelation.Rollid=Roll.Rollid and CadreInfo.CadreName = '%2' and Roll.RollName like '%%1%' and unit.unitname like '%%3%' ").arg(rname).arg(cname).arg(uname);
        c.exec(cadre);
        while (c.next()) {
            int rowcount=ui->RollInform->rowCount();
            ui->RollInform->insertRow(rowcount);//
            QString a=c.value(0).toString();
            ui->RollInform->setItem(rowcount,1,new QTableWidgetItem(a));
            QString b=c.value(1).toString();
            ui->RollInform->setItem(rowcount,0,new QTableWidgetItem(b));
            QString d=c.value(2).toString();
            ui->RollInform->setItem(rowcount,2,new QTableWidgetItem(d));
        }

    }




    int i=ui->RollInform->rowCount();
    if (i==0) {
        QMessageBox::critical(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("未能查询到结果!"));

//        QSqlQuery r;
//        QString roll=QString("select Rollname,RollDate,RollID from Roll where RollType='0'");
//        r.exec(roll);
//        while (r.next()) {
//            int rowcount=ui->RollInform->rowCount();
//            ui->RollInform->insertRow(rowcount);
//            QString a=r.value(0).toString();
//            ui->RollInform->setItem(rowcount,1,new QTableWidgetItem(a));
//            QString b=r.value(1).toString();
//            ui->RollInform->setItem(rowcount,0,new QTableWidgetItem(b));
//            QString c=r.value(2).toString();
//            ui->RollInform->setItem(rowcount,2,new QTableWidgetItem(c));
//}
      showroll();
    }


}


void Widget::on_EditRoll_clicked()
{
  int i=ui->RollInform->currentRow();
  if (i==-1) {
       QMessageBox::critical(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("请选中要编辑的名册!"));
  } else {

      editroll *er=new editroll;
      connect(this,SIGNAL(sendrollid(QString)),er,SLOT(receiverid(QString)));
      connect(er,SIGNAL(bbb()),this,SLOT(showrollslot()));
      emit sendrollid(ui->RollInform->item(i,2)->text());
      er->show();
  }

}

void Widget::on_CadreinRollNum_clicked()
{
    QSqlQuery query;
    QString total=QString("select count(CadreRollRelation.idnum) from CadreInfo,CadreRollRelation where CadreInfo.IDNum=CadreRollRelation.idnum ");
    query.exec(total);
    while (query.next()) {

        QString renshu=query.value(0).toString();
        ui->renshu->setText(QString(renshu));
        ui->dangqian->setText("共：");
    }
}



void Widget::on_shuchu_clicked()
{

//    int i =ui->RollInform->currentRow();
//    QString rollname=ui->RollInform->item(i,1)->text();

    QString shuchuneirong;
    shuchuneirong=ui->shuchuneirong->currentText();
    if (shuchuneirong=="当前单位名册") {
        int i =ui->RollInform->currentRow();


        if (i==-1) {
            QMessageBox::critical(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("请选中一个名册!"));
        } else {
            QString filepath = QFileDialog::getSaveFileName(this, tr("Save as xml"),QString(), "XML files (*.xml);;");
            if (filepath.isEmpty()) {
                return;
            }
            filepath += ".xml";

            QFile file(filepath);
            if (!file.open(QFile::WriteOnly | QIODevice::Text)) {
                return;
            }

            QString rname=ui->RollInform->item(i,1)->text();
            //创建xml文档对象
            QDomDocument doc;
            //创建xml头部格式 <?xml version='1.0' encoding='utf-8'?>
            QDomProcessingInstruction ins;
            ins = doc.createProcessingInstruction("xml", "version=\'1.0\' encoding=\'utf-8\'");
            //追加元素
            doc.appendChild(ins);

            //根节点元素
            QDomElement root = doc.createElement("当前单位名册");
            doc.appendChild(root);

            QDomElement rn = doc.createElement(rname);
            root.appendChild(rn);

            //创建文书内容子节点元素
            QDomElement docEmt = doc.createElement("单位列表");
            //把节点追加到根节点上
            rn.appendChild(docEmt);

            int rowu=ui->unit->rowCount();
            for (int i = 0; i < rowu; i++) {
                QDomElement unitEmt = doc.createElement("单位");
                docEmt.appendChild(unitEmt);
                QDomElement unitname = doc.createElement("单位名称");
                QDomElement shuoming = doc.createElement("说明");
                QDomElement type = doc.createElement("类型");
                QDomElement bianzhi = doc.createElement("编制情况");

                if(ui->unit->item(i,0)!=NULL)
                    {
                    QString unitText = ui->unit->item(i,0)->text();
                    QDomText text = doc.createTextNode(unitText);
                    unitname.appendChild(text);

                }

                if(ui->unit->item(i,1)!=NULL)
                    {
                    QString smtext = ui->unit->item(i,1)->text();
                    QDomText text = doc.createTextNode(smtext);
                    shuoming.appendChild(text);

                }

                if(ui->unit->item(i,3)!=NULL)
                    {
                    QString typeText = ui->unit->item(i,3)->text();
                    QDomText text = doc.createTextNode(typeText);
                    type.appendChild(text);

                }

                if(ui->unit->item(i,4)!=NULL)
                    {
                    QString bzText = ui->unit->item(i,4)->text();
                    QDomText text = doc.createTextNode(bzText);
                    bianzhi.appendChild(text);

                }
                unitEmt.appendChild(unitname);
                unitEmt.appendChild(shuoming);
                unitEmt.appendChild(type);
                unitEmt.appendChild(bianzhi);


            }

            QDomElement apEmt = doc.createElement("干部列表");
            rn.appendChild(apEmt);

            int rowc = ui->cadre->rowCount();
            for(int i=0;i<rowc;i++)
            {
                //干部列表
                QDomElement cdEmt = doc.createElement("干部");
                apEmt.appendChild(cdEmt);


                QDomElement cadreName = doc.createElement("姓名");
                QDomElement idnum = doc.createElement("证件号码");
                QDomElement post = doc.createElement("职务");
                QDomElement posttime = doc.createElement("任职时间");
                QDomElement rank = doc.createElement("级别");
                QDomElement tongji = doc.createElement("同级时间");
                QDomElement birth = doc.createElement("出生时间");
                QDomElement sex = doc.createElement("性别");
                QDomElement jiguan = doc.createElement("籍贯");
                QDomElement jobtime = doc.createElement("工作时间");


                if(ui->cadre->item(i,0)!=NULL)
                    {
                    QString nameText = ui->cadre->item(i,0)->text();
                    QDomText text = doc.createTextNode(nameText);
                    cadreName.appendChild(text);
                }

                if(ui->cadre->item(i,1)!=NULL)
                    {
                    QString idText = ui->cadre->item(i,1)->text();
                    QDomText text = doc.createTextNode(idText);
                    idnum.appendChild(text);

                }

                if(ui->cadre->item(i,2)!=NULL)
                    {
                    QString postText = ui->cadre->item(i,2)->text();
                    QDomText text = doc.createTextNode(postText);
                    post.appendChild(text);

                }

                if(ui->cadre->item(i,3)!=NULL)
                    {
                    QString ptText = ui->cadre->item(i,3)->text();
                    QDomText text = doc.createTextNode(ptText);
                    posttime.appendChild(text);

                }
                if(ui->cadre->item(i,4)!=NULL)
                    {
                    QString rankText = ui->cadre->item(i,4)->text();
                    QDomText text = doc.createTextNode(rankText);
                    rank.appendChild(text);

                }

                if(ui->cadre->item(i,5)!=NULL)
                    {
                    QString tjText = ui->cadre->item(i,5)->text();
                    QDomText text = doc.createTextNode(tjText);
                    tongji.appendChild(text);

                }
                if(ui->cadre->item(i,6)!=NULL)
                    {
                    QString birthText = ui->cadre->item(i,6)->text();
                    QDomText text = doc.createTextNode(birthText);
                    birth.appendChild(text);

                }
                if(ui->cadre->item(i,7)!=NULL)
                    {
                    QString sexText = ui->cadre->item(i,7)->text();
                    QDomText text = doc.createTextNode(sexText);
                    sex.appendChild(text);

                }




                if(ui->cadre->item(i,8)!=NULL)
                    {
                    QString jgText = ui->cadre->item(i,8)->text();
                    QDomText text = doc.createTextNode(jgText);
                    jiguan.appendChild(text);

                }



                if(ui->cadre->item(i,9)!=NULL)
                    {
                    QString jtText = ui->cadre->item(i,9)->text();
                    QDomText text = doc.createTextNode(jtText);
                    jobtime.appendChild(text);

                }

                cdEmt.appendChild(cadreName);
                cdEmt.appendChild(idnum);
                cdEmt.appendChild(post);
                cdEmt.appendChild(posttime);
                cdEmt.appendChild(rank);
                cdEmt.appendChild(tongji);
                cdEmt.appendChild(birth);
                cdEmt.appendChild(sex);
                cdEmt.appendChild(jiguan);
                cdEmt.appendChild(jobtime);


            }

            //保存
            QTextStream stream(&file); //文本流关联文件
            doc.save(stream, 4); //4 缩进字符
            file.close(); //关闭文件

            QMessageBox::information(this,"提示","导出成功！");


    }
        }
    if (shuchuneirong=="名册目录") {

        QString filepath = QFileDialog::getSaveFileName(this, tr("Save as xml"),QString(), "XML files (*.xml);;");
        if (filepath.isEmpty()) {
            return;
        }
        filepath += ".xml";

        QFile file(filepath);
        if (!file.open(QFile::WriteOnly | QIODevice::Text)) {
            return;
        }


        //创建xml文档对象
        QDomDocument doc;
        //创建xml头部格式 <?xml version='1.0' encoding='utf-8'?>
        QDomProcessingInstruction ins;
        ins = doc.createProcessingInstruction("xml", "version=\'1.0\' encoding=\'utf-8\'");
        //追加元素
        doc.appendChild(ins);

        //根节点元素
        QDomElement root = doc.createElement("名册目录");
        doc.appendChild(root);


        int rowc = ui->RollInform->rowCount();
        for(int i=0;i<rowc;i++)
        {

            QDomElement rollEmt = doc.createElement("名册");
            root.appendChild(rollEmt);

            QDomElement rolltime = doc.createElement("日期");
            QDomElement rollname = doc.createElement("名册名称");
            QDomElement rollnum = doc.createElement("名册编号");


            if(ui->RollInform->item(i,0)!=NULL)
                {
                QString rtText = ui->RollInform->item(i,0)->text();
                QDomText text = doc.createTextNode(rtText);
                rolltime.appendChild(text);

            }

            if(ui->RollInform->item(i,1)!=NULL)
                {
                QString rnText = ui->RollInform->item(i,1)->text();
                QDomText text = doc.createTextNode(rnText);
                rollname.appendChild(text);

            }

            if(ui->RollInform->item(i,2)!=NULL)
                {
                QString rnText = ui->RollInform->item(i,2)->text();
                QDomText text = doc.createTextNode(rnText);
                rollnum.appendChild(text);

            }


            rollEmt.appendChild(rolltime);
            rollEmt.appendChild(rollname);
            rollEmt.appendChild(rollnum);


        }

        //保存
        QTextStream stream(&file); //文本流关联文件
        doc.save(stream, 4); //4 缩进字符
        file.close(); //关闭文件

        QMessageBox::information(this,"提示","导出成功！");
    }
    if (shuchuneirong=="当前干部简介") {
        int row = ui->cadre->currentRow();
        if (row==-1) {
            QMessageBox::critical(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("请选中一名或多名干部!"));
        } else {


            QString filepath = QFileDialog::getSaveFileName(this, tr("Save as xml"),QString(), "XML files (*.xml);;");
            if (filepath.isEmpty()) {
                return;
            }
            filepath += ".xml";

            QFile file(filepath);
            if (!file.open(QFile::WriteOnly | QIODevice::Text)) {
                return;
            }


            //创建xml文档对象
            QDomDocument doc;
            //创建xml头部格式 <?xml version='1.0' encoding='utf-8'?>
            QDomProcessingInstruction ins;
            ins = doc.createProcessingInstruction("xml", "version=\'1.0\' encoding=\'utf-8\'");
            //追加元素
            doc.appendChild(ins);

            //根节点元素
            QDomElement root = doc.createElement("当前干部信息");
            doc.appendChild(root);
            QList<QTableWidgetSelectionRange>ranges = ui->cadre->selectedRanges();
          //  int rownum;

            //ui->cadre->setRowCount(0);

            int count=ranges.count();

            for(int i=0;i<count;i++)

            {

                int topRow=ranges.at(i).topRow();
                int bottomRow=ranges.at(i).bottomRow();
                for(int j=topRow;j<=bottomRow;j++)
                {
                    qDebug()<<j;





//            int i = ui->cadre->currentRow();

                //干部列表
                QDomElement cdEmt = doc.createElement("干部信息");
                root.appendChild(cdEmt);


                QDomElement cadreName = doc.createElement("姓名");
                QDomElement idnum = doc.createElement("证件号码");
                QDomElement post = doc.createElement("职务");
                QDomElement posttime = doc.createElement("任职时间");
                QDomElement rank = doc.createElement("级别");
                QDomElement tongji = doc.createElement("同级时间");
                QDomElement birth = doc.createElement("出生时间");
                QDomElement sex = doc.createElement("性别");
                QDomElement jiguan = doc.createElement("籍贯");
                QDomElement jobtime = doc.createElement("工作时间");


                if(ui->cadre->item(j,0)!=NULL)
                    {
                    QString nameText = ui->cadre->item(j,0)->text();
                    QDomText text = doc.createTextNode(nameText);
                    cadreName.appendChild(text);
                }

                if(ui->cadre->item(j,1)!=NULL)
                    {
                    QString idText = ui->cadre->item(j,1)->text();
                    QDomText text = doc.createTextNode(idText);
                    idnum.appendChild(text);

                }

                if(ui->cadre->item(j,2)!=NULL)
                    {
                    QString postText = ui->cadre->item(j,2)->text();
                    QDomText text = doc.createTextNode(postText);
                    post.appendChild(text);

                }

                if(ui->cadre->item(j,3)!=NULL)
                    {
                    QString ptText = ui->cadre->item(j,3)->text();
                    QDomText text = doc.createTextNode(ptText);
                    posttime.appendChild(text);

                }
                if(ui->cadre->item(j,4)!=NULL)
                    {
                    QString rankText = ui->cadre->item(j,4)->text();
                    QDomText text = doc.createTextNode(rankText);
                    rank.appendChild(text);

                }

                if(ui->cadre->item(j,5)!=NULL)
                    {
                    QString tjText = ui->cadre->item(j,5)->text();
                    QDomText text = doc.createTextNode(tjText);
                    tongji.appendChild(text);

                }
                if(ui->cadre->item(j,6)!=NULL)
                    {
                    QString birthText = ui->cadre->item(j,6)->text();
                    QDomText text = doc.createTextNode(birthText);
                    birth.appendChild(text);

                }
                if(ui->cadre->item(j,7)!=NULL)
                    {
                    QString sexText = ui->cadre->item(j,7)->text();
                    QDomText text = doc.createTextNode(sexText);
                    sex.appendChild(text);

                }

                if(ui->cadre->item(j,8)!=NULL)
                    {
                    QString jgText = ui->cadre->item(j,8)->text();
                    QDomText text = doc.createTextNode(jgText);
                    jiguan.appendChild(text);

                }

                if(ui->cadre->item(j,9)!=NULL)
                    {
                    QString jtText = ui->cadre->item(j,9)->text();
                    QDomText text = doc.createTextNode(jtText);
                    jobtime.appendChild(text);

                }

                cdEmt.appendChild(cadreName);
                cdEmt.appendChild(idnum);
                cdEmt.appendChild(post);
                cdEmt.appendChild(posttime);
                cdEmt.appendChild(rank);
                cdEmt.appendChild(tongji);
                cdEmt.appendChild(birth);
                cdEmt.appendChild(sex);
                cdEmt.appendChild(jiguan);
                cdEmt.appendChild(jobtime);






                }
            }

            //保存
            QTextStream stream(&file); //文本流关联文件
            doc.save(stream, 4); //4 缩进字符
            file.close(); //关闭文件

            QMessageBox::information(this,"提示","导出成功！");



        }


    }
    if (shuchuneirong=="当前单位干部简介") {

        int i=ui->unit->currentRow();
        if (i==-1) {
            QMessageBox::critical(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("请选中要查看的单位!"));
        } else {
            QString unitname;
            QString unitname_1;
            QList<QTableWidgetSelectionRange>ranges = ui->unit->selectedRanges();

            int count=ranges.count();

            for(int i=0;i<count;i++)

            {

                int topRow=ranges.at(i).topRow();
                int bottomRow=ranges.at(i).bottomRow();
                for(int j=topRow;j<=bottomRow;j++)
                {
                    unitname=ui->unit->item(j,0)->text();

                    unitname_1=unitname_1+" "+unitname;
                }

            }

            qDebug()<<unitname_1;

            QString filepath = QFileDialog::getSaveFileName(this, tr("Save as xml"),QString(), "XML files (*.xml);;");
            if (filepath.isEmpty()) {
                return;
            }
            filepath += ".xml";

            QFile file(filepath);
            if (!file.open(QFile::WriteOnly | QIODevice::Text)) {
                return;
            }


            //创建xml文档对象
            QDomDocument doc;
            //创建xml头部格式 <?xml version='1.0' encoding='utf-8'?>
            QDomProcessingInstruction ins;
            ins = doc.createProcessingInstruction("xml", "version=\'1.0\' encoding=\'utf-8\'");
            //追加元素
            doc.appendChild(ins);

            //根节点元素
            QDomElement root = doc.createElement("当前单位干部信息");
            doc.appendChild(root);
            QDomElement unit = doc.createElement(unitname_1);
            root.appendChild(unit);

            int rownum = ui->cadre->rowCount();
            for (int i = 0; i < rownum; i++) {


                //干部列表
                QDomElement cdEmt = doc.createElement("干部信息");
                unit.appendChild(cdEmt);


                QDomElement cadreName = doc.createElement("姓名");
                QDomElement idnum = doc.createElement("证件号码");
                QDomElement post = doc.createElement("职务");
                QDomElement posttime = doc.createElement("任职时间");
                QDomElement rank = doc.createElement("级别");
                QDomElement tongji = doc.createElement("同级时间");
                QDomElement birth = doc.createElement("出生时间");
                QDomElement sex = doc.createElement("性别");
                QDomElement jiguan = doc.createElement("籍贯");
                QDomElement jobtime = doc.createElement("工作时间");


                if(ui->cadre->item(i,0)!=NULL)
                    {
                    QString nameText = ui->cadre->item(i,0)->text();
                    QDomText text = doc.createTextNode(nameText);
                    cadreName.appendChild(text);
                }

                if(ui->cadre->item(i,1)!=NULL)
                    {
                    QString idText = ui->cadre->item(i,1)->text();
                    QDomText text = doc.createTextNode(idText);
                    idnum.appendChild(text);

                }

                if(ui->cadre->item(i,2)!=NULL)
                    {
                    QString postText = ui->cadre->item(i,2)->text();
                    QDomText text = doc.createTextNode(postText);
                    post.appendChild(text);

                }

                if(ui->cadre->item(i,3)!=NULL)
                    {
                    QString ptText = ui->cadre->item(i,3)->text();
                    QDomText text = doc.createTextNode(ptText);
                    posttime.appendChild(text);

                }
                if(ui->cadre->item(i,4)!=NULL)
                    {
                    QString rankText = ui->cadre->item(i,4)->text();
                    QDomText text = doc.createTextNode(rankText);
                    rank.appendChild(text);

                }

                if(ui->cadre->item(i,5)!=NULL)
                    {
                    QString tjText = ui->cadre->item(i,5)->text();
                    QDomText text = doc.createTextNode(tjText);
                    tongji.appendChild(text);

                }
                if(ui->cadre->item(i,6)!=NULL)
                    {
                    QString birthText = ui->cadre->item(i,6)->text();
                    QDomText text = doc.createTextNode(birthText);
                    birth.appendChild(text);

                }
                if(ui->cadre->item(i,7)!=NULL)
                    {
                    QString sexText = ui->cadre->item(i,7)->text();
                    QDomText text = doc.createTextNode(sexText);
                    sex.appendChild(text);

                }

                if(ui->cadre->item(i,8)!=NULL)
                    {
                    QString jgText = ui->cadre->item(i,8)->text();
                    QDomText text = doc.createTextNode(jgText);
                    jiguan.appendChild(text);

                }

                if(ui->cadre->item(i,9)!=NULL)
                    {
                    QString jtText = ui->cadre->item(i,9)->text();
                    QDomText text = doc.createTextNode(jtText);
                    jobtime.appendChild(text);

                }

                cdEmt.appendChild(cadreName);
                cdEmt.appendChild(idnum);
                cdEmt.appendChild(post);
                cdEmt.appendChild(posttime);
                cdEmt.appendChild(rank);
                cdEmt.appendChild(tongji);
                cdEmt.appendChild(birth);
                cdEmt.appendChild(sex);
                cdEmt.appendChild(jiguan);
                cdEmt.appendChild(jobtime);

}


            //保存
            QTextStream stream(&file); //文本流关联文件
            doc.save(stream, 4); //4 缩进字符
            file.close(); //关闭文件

            QMessageBox::information(this,"提示","导出成功！");




    }
        }
    if (shuchuneirong=="全部单位干部简介") {
        int i =ui->RollInform->currentRow();
        if (i==-1) {
            QMessageBox::critical(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("请选中一个名册"));
        } else {

            QString rollname=ui->RollInform->item(i,1)->text();

            QString filepath = QFileDialog::getSaveFileName(this, tr("Save as xml"),QString(), "XML files (*.xml);;");
            if (filepath.isEmpty()) {
                return;
            }
            filepath += ".xml";

            QFile file(filepath);
            if (!file.open(QFile::WriteOnly | QIODevice::Text)) {
                return;
            }


            //创建xml文档对象
            QDomDocument doc;
            //创建xml头部格式 <?xml version='1.0' encoding='utf-8'?>
            QDomProcessingInstruction ins;
            ins = doc.createProcessingInstruction("xml", "version=\'1.0\' encoding=\'utf-8\'");
            //追加元素
            doc.appendChild(ins);

            //根节点元素
            QDomElement root = doc.createElement("全部单位干部信息");
            doc.appendChild(root);
            QDomElement roll = doc.createElement(rollname);
            root.appendChild(roll);

            int rownum = ui->cadre->rowCount();
            for (int i = 0; i < rownum; i++) {


                //干部列表
                QDomElement cdEmt = doc.createElement("干部信息");
                roll.appendChild(cdEmt);


                QDomElement cadreName = doc.createElement("姓名");
                QDomElement idnum = doc.createElement("证件号码");
                QDomElement post = doc.createElement("职务");
                QDomElement posttime = doc.createElement("任职时间");
                QDomElement rank = doc.createElement("级别");
                QDomElement tongji = doc.createElement("同级时间");
                QDomElement birth = doc.createElement("出生时间");
                QDomElement sex = doc.createElement("性别");
                QDomElement jiguan = doc.createElement("籍贯");
                QDomElement jobtime = doc.createElement("工作时间");


                if(ui->cadre->item(i,0)!=NULL)
                    {
                    QString nameText = ui->cadre->item(i,0)->text();
                    QDomText text = doc.createTextNode(nameText);
                    cadreName.appendChild(text);
                }

                if(ui->cadre->item(i,1)!=NULL)
                    {
                    QString idText = ui->cadre->item(i,1)->text();
                    QDomText text = doc.createTextNode(idText);
                    idnum.appendChild(text);

                }

                if(ui->cadre->item(i,2)!=NULL)
                    {
                    QString postText = ui->cadre->item(i,2)->text();
                    QDomText text = doc.createTextNode(postText);
                    post.appendChild(text);

                }

                if(ui->cadre->item(i,3)!=NULL)
                    {
                    QString ptText = ui->cadre->item(i,3)->text();
                    QDomText text = doc.createTextNode(ptText);
                    posttime.appendChild(text);

                }
                if(ui->cadre->item(i,4)!=NULL)
                    {
                    QString rankText = ui->cadre->item(i,4)->text();
                    QDomText text = doc.createTextNode(rankText);
                    rank.appendChild(text);

                }

                if(ui->cadre->item(i,5)!=NULL)
                    {
                    QString tjText = ui->cadre->item(i,5)->text();
                    QDomText text = doc.createTextNode(tjText);
                    tongji.appendChild(text);

                }
                if(ui->cadre->item(i,6)!=NULL)
                    {
                    QString birthText = ui->cadre->item(i,6)->text();
                    QDomText text = doc.createTextNode(birthText);
                    birth.appendChild(text);

                }
                if(ui->cadre->item(i,7)!=NULL)
                    {
                    QString sexText = ui->cadre->item(i,7)->text();
                    QDomText text = doc.createTextNode(sexText);
                    sex.appendChild(text);

                }

                if(ui->cadre->item(i,8)!=NULL)
                    {
                    QString jgText = ui->cadre->item(i,8)->text();
                    QDomText text = doc.createTextNode(jgText);
                    jiguan.appendChild(text);

                }

                if(ui->cadre->item(i,9)!=NULL)
                    {
                    QString jtText = ui->cadre->item(i,9)->text();
                    QDomText text = doc.createTextNode(jtText);
                    jobtime.appendChild(text);

                }

                cdEmt.appendChild(cadreName);
                cdEmt.appendChild(idnum);
                cdEmt.appendChild(post);
                cdEmt.appendChild(posttime);
                cdEmt.appendChild(rank);
                cdEmt.appendChild(tongji);
                cdEmt.appendChild(birth);
                cdEmt.appendChild(sex);
                cdEmt.appendChild(jiguan);
                cdEmt.appendChild(jobtime);

}


            //保存
            QTextStream stream(&file); //文本流关联文件
            doc.save(stream, 4); //4 缩进字符
            file.close(); //关闭文件

            QMessageBox::information(this,"提示","导出成功！");



}

    }









}
